﻿//'
//' DotNetNuke® - http://www.dotnetnuke.com
//' Copyright (c) 2002-2008
//' by DotNetNuke Corporation
//'
//' Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated 
//' documentation files (the "Software"), to deal in the Software without restriction, including without limitation 
//' the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and 
//' to permit persons to whom the Software is furnished to do so, subject to the following conditions:
//'
//' The above copyright notice and this permission notice shall be included in all copies or substantial portions 
//' of the Software.
//'
//' THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED 
//' TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL 
//' THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF 
//' CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER 
//' DEALINGS IN THE SOFTWARE.
//'
var MSG_CHATUSERLIST_TYPE_UNDEFINED = '0';
var MSG_CHATUSERLIST_TYPE_POLL      = '1';
var MSG_CHATUSERLIST_TYPE_PERSONAL  = '2';

var XML_EMPTYCHATLISTMESSAGE   = '<DnnChatUserListPoll TZ="{TZ}" PM="{PM}" MT="{MT}"/>'

function pollServerForChatUserList(type,value)
{
    var _personalMessage = '';
    var xmlDoc = new dnn.xml.createDocument();

    // Load the result-xml document
    xmlDoc.loadXml(XML_EMPTYCHATLISTMESSAGE);

    // Get the message element
    var elmMsg = xmlDoc.rootNode(); //at this point we're sure there's only one
    //set the attributes
    elmMsg.setAttribute("MT", type);
    elmMsg.setAttribute("TZ", eval('timeZone_' + chatListModuleID));

    if (type == MSG_CHATUSERLIST_TYPE_PERSONAL)
    {
        _personalMessage = value;
    }
    elmMsg.setAttribute("PM", _personalMessage);
    
    return xmlDoc.getXml();        
}

function ChangePersonalMessage(element)
{
        var _personalMessage = '';
        _personalMessage = element.value;
        element.valueOf = ""; 
        return pollServerForChatUserList(MSG_CHATUSERLIST_TYPE_PERSONAL,_personalMessage);
}

function receiveChatUserListMessage(result, context)
{
    try
    {
        //Context is filled with "ChatUserList"
        var objDiv = context;
       
        if ( objDiv.hasChildNodes() )
        {
            while ( objDiv.childNodes.length >= 1 )
            {
                objDiv.removeChild( objDiv.firstChild );       
            } 
        }
   
         var dom = new dnn.xml.createDocument();
        // Load the result-xml document
        dom.loadXml(result);

        // Get the top node
        var topNode = dom.childNodes(0);
        
        var showJoined = new Boolean(topNode.getAttribute('JOIN'));
        var showLastMessage = new Boolean(topNode.getAttribute('LMSG'));
        var showPolling = new Boolean(topNode.getAttribute('POLL'));
        
        var messageNode;
        var userID = "";
        var nickName = ""; 
        var personalMessage = ""; 
        var polling;  
        var message; 
        var allowedToChat = new Boolean(); 
        
        var _row, _col1, _col2, _text1, _text2;
        var _table = document.createElement("table");
        var _thead = document.createElement("thead");
        var _tbody = document.createElement("tbody");

    
 
        _headerrow = document.createElement("tr");
        _headerrow.appendChild(GetUserListCell("Name", "th", "culuh"));
        if (showJoined==true ) {_headerrow.appendChild(GetUserListCell("Joined", "th", "culuh"));}
        if (showLastMessage==true) {_headerrow.appendChild(GetUserListCell("Last msg", "th", "culuh"));}
        if (showPolling==true) {_headerrow.appendChild(GetUserListCell("Last poll", "th", "culuh"));}
        
        _thead.appendChild(_headerrow);
       
       
        // Loop through the child elements
        for(var i=0; i<=topNode.childNodeCount()-1; i++)
        {
            messageNode = topNode.childNodes(i);
        
            // Get the element that the Node is about
            nickName = messageNode.getAttribute('NICK');
            personalMessage = messageNode.getAttribute('PEME');
            if (personalMessage != null && personalMessage.length > 0)  
            {
                nickName =  nickName + " - " + personalMessage;
            }
            joined = messageNode.getAttribute('JOIN');
            message = messageNode.getAttribute('LMSG');
            polling = messageNode.getAttribute('POLL');
            allowedToChat = messageNode.getAttribute('ALTC');

            _row = document.createElement("tr");
            if (allowedToChat==true)
            {
                _row.appendChild(GetUserListCell(nickName, "td", "culurc"));
            }
            else
            {
                _row.appendChild(GetUserListCell(nickName, "td", "culurr"));
            } 
            if (showJoined==true) {_row.appendChild(GetUserListCell(joined, "td", "culur"));}
            if (showLastMessage==true) {_row.appendChild(GetUserListCell(message, "td", "culur"));}
            if (showPolling==true) {_row.appendChild(GetUserListCell(polling, "td", "culur"));}       
            _tbody.appendChild(_row);

        }

        _table.appendChild(_thead);
        _table.appendChild(_tbody);
        _table.className = "cultbl";
        objDiv.appendChild(_table);

        
    }
    catch(exc)
    {
        var newDv = document.createElement("div");
        newDv.innerHTML = "<span class='NormalRed'>Client-error: " + exc.message + "</span><br>";
        context.appendChild(newDv);
    }
}

function receiveChatUserListMessageError(result,context)
{
    var newDiv = document.createElement("div");
    newDiv.innerHTML = "<span class='NormalRed'>Server-error: " + result + "</span><br>";
    context.appendChild(newDiv);
}


function GetUserListCell(value,type,css)
{
    var _col, _text;
    _col = document.createElement(type);
    _col.className =css;
    _text = document.createTextNode(value);
    _col.appendChild(_text);
    return _col;
}

